<?php defined('SYSPATH') or die('No direct script access.');

//拼团
class Controller_Pc_Api_V2_Together extends Controller_Pc_Api_v2_Base
{
    // 返回结果
    private $result;

    /**
     * 参数验证对象
     * @var Request_Api_V2_Together
     */
    private $validation;

    /**
     * 服务
     * @var Service_Api_Together
     */
    private $service;

    public function before()
    {
        parent::before();

        // 参数验证对象
        $this->validation = Request_Api_V2_Factory::create('together', $this->request_params);

        // 服务
        $this->service = new Service_Api_Together();

        // 返回结果
        $this->result = array(
            'status' => true,//默认状态为成功
            'msg' => '操作成功',//默认的提示消息
            'data' => array()
        );
    }


    // 查询拼团
    public function action_query()
    {
        try {
            // 验证参数
            $this->validation->query()->check();

            $page       = Arr::get($this->request_params, 'page', 1);
            $pagesize   = Arr::get($this->request_params, 'pagesize', 10);
            $status     = Arr::get($this->request_params, 'status', 1);
            $params               = array(
                'page'       => $page,
                'pagesize'   => $pagesize,
                'status'     => $status,
                'keyword'    => Arr::get($this->request_params, 'keyword', ''),
                'show_total' => true
            );
            $this->result['data'] = $this->service->query($params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 获取拼团详情
    public function action_detail()
    {
        try {
            // 验证参数
            $this->validation->detail()->check();

            $id   = Arr::get($this->request_params, 'id');
            $data = $this->service->detail($id,true,$this->request_params);
            // 未开启凑团返回凑团空数组
            if ($data['gather_together_is_open'] != 1) {
                $data['join_list'] = array();
            }
            $this->result['data'] = $data;
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 获取可凑团列表
    public function action_pending_join_list()
    {
        try {
            // 验证参数
            $this->validation->pending_join_list()->check();

            $id       = Arr::get($this->request_params, 'id');

            $this->result['data'] = $this->service->pending_join_list($id);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 获取预订限制
    public function action_order_limit()
    {
        try {
            // 验证参数
            $this->validation->order_limit()->check();

            $id       = Arr::get($this->request_params, 'id');

            // 优惠券使用限制
            $allow_discount_type =  $this->service->order_limit($id);

            $this->result['data'] = array('allow_discount_type' => $allow_discount_type);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }


    // 会员的拼团
    public function action_get_member_join()
    {
        try {
            // 验证参数
            $this->validation->get_member_join()->check();

            $page     = Arr::get($this->request_params, 'page', 1);    // 当前页
            $pagesize = Arr::get($this->request_params, 'pagesize', 10); // 每页数量
            $type     = Arr::get($this->request_params, 'type', ''); // 拼团状态 1 拼团中 2 已成团

            $params = array(
                'type' => $type,
                'page' => $page,
                'pagesize' => $pagesize
            );

            $data   = $this->service->get_member_join($this->mid, $params);

            $this->result['data'] = $data;
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 会员的拼团详情
    public function action_get_member_join_detail()
    {
        try {
            $this->validation->get_member_join_detail()->check();

            $join_id = Arr::get($this->request_params, 'join_id');

            $data   = $this->service->get_member_join_detail($this->mid, $join_id);

            $this->result['data'] = $data;
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 拼团分享信息
    public function action_get_share_info()
    {
        try {
            $this->validation->get_share_info()->check();

            $data   = $this->service->get_share_info($this->mid, $this->request_params);
            $this->result['data'] = $data;
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    public function after()
    {
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }
}
